package com.loong.nnm.utils;

import com.loong.nnm.entity.netconf.NetconfBuilderParameters;
import net.juniper.netconf.*;
import org.springframework.stereotype.Service;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

@Service
public class NetconfProtocolUtils {

//    private static final String HOSTNAME = "192.168.12.112";
//    private static final String USERNAME = "admin";
//    private static final String PASSWORD = "admin123!!!";

    private static String HOSTNAME;
    private static String USERNAME;
    private static String PASSWORD;

    private static int NETCONF_PORT = 830;

    private Device device;

    public Device getDevice() {
        return device;
    }

    //全局会话信息
    private static NetconfSession session;

    public int setUp(NetconfBuilderParameters parameters) {
        HOSTNAME = parameters.getHost();
        USERNAME = parameters.getUsername();
        PASSWORD = parameters.getPassword();
        NETCONF_PORT = parameters.getPort();

        return createDevices();
    }

    public int createDevices() {
        try {
            device = Device.builder().hostName(HOSTNAME)
                    .userName(USERNAME)
                    .password(PASSWORD)
                    .port(NETCONF_PORT)
                    .strictHostKeyChecking(false)
                    .hostKeysFileName(null)
                    .timeout(30000)
                    .build();
            device.connect();
        } catch (NetconfException e) {
            e.printStackTrace();
        }
        return device.isConnected() ? 1 : 0;
    }

    public XML getXmlContent() {
        XMLBuilder xmlBuilder = null;
        XML get = null;
        try {
            xmlBuilder = new XMLBuilder();
            get = xmlBuilder.createNewRPC("get", "filter");
            get.setAttribute("type", "subtree");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return get;
    }


    public boolean isConnected(String node) {
        if (device.getHostName().equals(node)) {
            return device.isConnected();
        }
        return false;
    }

    public boolean isOk(String node) {
        if (device.getHostName().equals(node)) {
            return device.isOK();
        }
        return false;
    }

    public XML send(String deviceHOSTNAME, String deviceUSERNAME, String devicePASSWORD, int deviceNETCONFPORT
            , String xmlContent) throws NetconfException {
        Device device = null;
        XML xmlReply = null;

        try {
            device = Device.builder().hostName(deviceHOSTNAME)
                    .userName(deviceUSERNAME)
                    .password(devicePASSWORD)
                    .port(deviceNETCONFPORT)
                    .strictHostKeyChecking(false)
                    .hostKeysFileName(null)
                    .build();
            device.connect();

            xmlReply = device.executeRPC(xmlContent);
            device.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new NetconfException("报文发送失败");
        } finally {
            if (null != device) {
                device.close();
            }
        }
        return xmlReply;
    }

    /**
     * 要发送的xml报文 例如：
     * <get>
     * <filter type="subtree">
     * <users xmlns="http://netconfcentral.org/ns/user">
     * <user>
     * <name>admin</name>
     * </user>
     * </users>
     * </filter>
     * </get>
     *
     * @param xmlContent
     * @return
     */
    public XML send(String xmlContent) {
        XML xmlReply = null;
        try {
            xmlReply = device.executeRPC(xmlContent);

        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw new NetconfException("报文发送失败");
            } catch (NetconfException netconfException) {
                netconfException.printStackTrace();
            }
        } finally {
            if (null != device) {
                device.close();
            }
        }
        return xmlReply;
    }

    public XML send(XML xmlContent) {
        return send(xmlContent.toString());
    }

    //断开ssh连接，结束netconf服务
    public void close(String hostName) {
        if (device.getHostName().equals(hostName) && device.isConnected()) {
            device.close();
        }
    }

    //重启设备
    public void reboot(String hostName) {
        if (device.getHostName().equals(hostName) && device.isConnected()) {
            try {
                device.reboot();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
